#
# @lc app=leetcode.cn id=108 lang=python
#
# [108] 将有序数组转换为二叉搜索树
#

# @lc code=start
# Definition for a binary tree node.
class TreeNode(object):
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right
        
class Solution(object):
    def sortedArrayToBST(self, nums):
        """
        :type nums: List[int]
        :rtype: Optional[TreeNode]
        """
        root = self.loop(nums)
        return root

    def loop(self, nums):
        if not nums:
            return None
        root_index = len(nums)//2
        root = TreeNode(nums[root_index])
        root.left = self.loop(nums[:root_index])
        root.right = self.loop(nums[root_index+1:])
        return root
        
# @lc code=end

